/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui.genomemap.outputModel;

import gui.genomemap.inputModel.InputModel;

import java.awt.Component;
import java.io.Serializable;
import java.util.Hashtable;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

/**
 * the general output model for FlexWorker.
 * @author Susanta Tewari
 * @version 1.0
 * @created 20-Nov-2007 7:38:26 PM
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class OutputModel implements Serializable, Comparable<OutputModel> {

    /**
     * the input model of the worker of which this is an output model. This input
     * model was used to instantiate this output model. A subclass of this input model
     * will be instantiated.
     */
    @OneToOne(cascade = CascadeType.ALL)
    private InputModel inputModel = null;

    /** Field description */
    private String log = "";

    /** Field description */
    private String modelStat = "";

    /**
     * key is the static final int value for the corresponding figure
     */
    @Transient
    private Map<Integer, Component> figuresMap = new Hashtable<>();

    /** Field description */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    /**
     * Holds value of property editable.
     */
    private boolean editable;

    /**
     * Constructs ...
     */
    public OutputModel() {}

    /**
     * 1. by linkage group 1 to 7
     * 2. larger the order sooner it comes,so has to be smaller in ascending.
     * 3. others will be implemented in the respective subclasses but the above
     *   two will be first evaluated using <CODE>super</CODE>.
     *   Note that TreeSet will order in ascending order. We want chids from 1..7
     *   then bigger orders followed.
     */
    @Override
    public int compareTo(OutputModel outputModel) {

        int myLinkageGroup  = getInputModel().getLinkageGroup();
        int hisLinkageGroup = outputModel.getInputModel().getLinkageGroup();
        int chDiff          = myLinkageGroup - hisLinkageGroup;

        if (chDiff == 0) {


            // same linkage group. now consider the order
            int myOrderCount   = getInputModel().getOrderCount();
            int hisOrderCount  = outputModel.getInputModel().getOrderCount();
            int orderCountDiff = myOrderCount - hisOrderCount;

            return (-1) * orderCountDiff;
        }

        return chDiff;
    }

    @Override
    public int hashCode() {
        return getInputModel().hashCode() + getLog().hashCode() + getModelStat().hashCode();
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof OutputModel) {

            OutputModel workerOutputModel = (OutputModel) obj;

            return getInputModel().equals(workerOutputModel.getInputModel())
                   && getLog().equals(workerOutputModel.getLog())
                   && getModelStat().equals(workerOutputModel.getModelStat());
        }

        return false;
    }

    public static String[] getOutputModelNames() {

        String[] modelNames = { "ComGLHoodOModel", "ComPLHoodOModel", "ComJLHoodOModel",
                                "AnnealOModel", "ProgCurveOModel" };

        return modelNames;
    }

    public static Class[] getOutputModelClasses() {

        Class[] classes = { ComGLHoodOModel.class, ComPLHoodOModel.class, ComJLHoodOModel.class,
                            AnnealOModel.class, ProgCurveOModel.class };

        return classes;
    }

    /**
     * the input model of the worker of which this is an output model. This input
     * model was used to instantiate this output model. A subclass of this input model
     * will be instantiated.
     */
    public InputModel getInputModel() {
        return inputModel;
    }

    /**
     * the input model of the worker of which this is an output model. This input
     * model was used to instantiate this output model. A subclass of this input model
     * will be instantiated.
     *
     * @param newVal
     */
    public void setInputModel(InputModel newVal) {
        inputModel = newVal;
    }

    /**
     * Getter for property editable.
     * @return Value of property editable.
     */
    public boolean isEditable() {
        return this.editable;
    }

    /**
     * Setter for property editable.
     * @param editable New value of property editable.
     */
    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public String getLog() {
        return log;
    }

    public void setLog(String log) {
        this.log = log;
    }

    public String getModelStat() {
        return modelStat;
    }

    public void setModelStat(String modelStat) {
        this.modelStat = modelStat;
    }

    public Map<Integer, Component> getFiguresMap() {
        return figuresMap;
    }

    public void setFiguresMap(Map<Integer, Component> figuresMap) {
        this.figuresMap = figuresMap;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
